package com.jiazhong.basic.pack0724;

import lombok.SneakyThrows;

import java.io.*;

public class DemoF {

    // 字节流的写入缓冲
    @SneakyThrows
    private static void a() {
        File file = new File("/Users/dailyblue/workspace/io/1.txt");
        // 创建了一个字节写入流
        OutputStream in = new FileOutputStream(file);
        // 创建缓冲流
        BufferedOutputStream bis = new BufferedOutputStream(in);
        bis.write("hello".getBytes());
        bis.flush();
        bis.close();
    }

    // 字节流的读取缓冲
    @SneakyThrows
    private static void b1() {
        File file = new File("/Users/dailyblue/workspace/io/2025.07.24.17.23.mp4");
        InputStream in = new FileInputStream(file);
        long start = System.currentTimeMillis();
        byte[] buffer = new byte[1024];
        while (true) {
            int len = in.read(buffer);
            if (len == -1) {
                break;
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("总共花费了：" + (end - start) + "毫秒");
        in.close();
    }

    @SneakyThrows
    private static void b2() {
        File file = new File("/Users/dailyblue/workspace/io/2025.07.24.17.23.mp4");
        InputStream in = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(in);
        long start = System.currentTimeMillis();
        byte[] buffer = new byte[1024];
        while (true) {
            int len = bis.read(buffer);
            if (len == -1) {
                break;
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("总共花费了：" + (end - start) + "毫秒");
        in.close();
    }

    // 字符流的写入缓冲
    @SneakyThrows
    private static void c() {
        File file = new File("/Users/dailyblue/workspace/io/1.txt");
        Writer writer = new FileWriter(file);
        BufferedWriter bw = new BufferedWriter(writer);
        bw.write("你好，我是张三，我是使用缓冲流写入的数据");
        bw.flush();
    }

    // 字符流的读取缓冲
    @SneakyThrows
    private static void d1() {
        File file = new File("/Users/dailyblue/workspace/io/2.txt");
        Reader reader = new FileReader(file);
        char[] buffer = new char[10];
        long start = System.currentTimeMillis();
        while (true) {
            int len = reader.read(buffer);
            if (len == -1) {
                break;
            }
            System.out.print(new String(buffer, 0, len));
        }
        System.out.println();
        long end = System.currentTimeMillis();
        System.out.println("总共花费了：" + (end - start) + "毫秒");
        reader.close();
    }

    @SneakyThrows
    private static void d2() {
        File file = new File("/Users/dailyblue/workspace/io/2.txt");
        Reader reader = new FileReader(file);
        BufferedReader br = new BufferedReader(reader);
        long start = System.currentTimeMillis();
        while (true) {
            String line = br.readLine();
            if (line == null) {
                break;
            }
            System.out.println(line);
        }
        System.out.println();
        long end = System.currentTimeMillis();
        System.out.println("总共花费了：" + (end - start) + "毫秒");
        reader.close();
    }

    public static void main(String[] args) {
        d1();
    }
}
